using UnityEngine;
using System.Collections;

public class Item : MonoBehaviour {
	bool m_got = false;																				//物品已经在被拾取中
	public GameObject firework;																	//穿透时爆发出的烟火。

	public static int m_gravity = 1;                                                  //重力速度
	Transform m_transform;                                                              //当前物体缓存

	public enum ITEM_TYPE { bomb , infinite_birds };
	public ITEM_TYPE m_itemType;

	void Start()
	{
		m_transform = this.transform;
		InvokeRepeating("CheckDestroy" , 3.0f , 1f);
	}

	void Update()
	{
		if (!GameManager.IsPlaying())
		{
			return;
		}

		DoAction();                                        //执行动作

	}

	
	void OnTriggerEnter(Collider enterObject)
	{
		if (enterObject.tag.Equals("Player_Bird") || enterObject.tag.Equals("Player_Attack_Area"))
		{
			if (m_got)
				return;
			
			m_got = true;

			//Add birds:
			GameManager.AddFish();

			if (m_itemType == ITEM_TYPE.bomb)
			{
				//Add item:
				GameManager.AddBomb();
			}
			else if (m_itemType == ITEM_TYPE.infinite_birds)
			{
				GameManager.UseItem(m_itemType);
			}
			

			//Play sound:
			SoundManager.Instance.PlayAudio(SoundManager.AUDIOTYPE.GET_ITEM);

			Instantiate(firework , m_transform.position , Quaternion.identity);

			Destroy(this.gameObject);
		}
		
	}

	//**************************************************************************
	//                                         DoAction()
	//执行动作
	//**************************************************************************
	Vector3 rotateAngel = new Vector3(0 , 0 , 2);							//旋转方向，受rotateRight影响，左右摇摆
	bool rotateRight = false;
	protected void DoAction()
	{
		//Rotation:
		m_transform.Rotate(rotateAngel * (rotateRight ? 1 : -1));
		m_transform.GetComponent<Rigidbody>().AddForce(0,m_gravity * -0.5F , 0 );
		if (m_transform.rotation.z < -0.3)
			rotateRight = true;
		else if (m_transform.rotation.z > 0.3)
			rotateRight = false;
	}


	


	
	//**************************************************************************
	//                                      销毁
	//  
	//**************************************************************************
	protected void CheckDestroy()
	{
		if (m_transform.position.y <= -13)
		{
			Destroy(this.gameObject);
		}
	}

}
